package cn.hw.sso.util;

import com.alibaba.fastjson.JSONObject;
import org.springframework.http.*;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.ResponseErrorHandler;
import org.springframework.web.client.RestTemplate;

import javax.net.ssl.*;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.Socket;
import java.security.cert.X509Certificate;

public class RpcUtil {
    private RpcUtil() {
    }


    /**
     * 调用http请求
     *
     * @param url    路径
     * @param data   参数数据
     * @param header 请求头数据
     * @param method 方法
     * @param isRaw  是否是原生
     * @return 返回的对象
     */
    public static <T> T httpInvoke(
            String url,
            JSONObject data,
            JSONObject header,
            HttpMethod method,
            boolean isRaw,
            Class<T> c) {
        System.out.println(url);
        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        requestFactory.setConnectTimeout(10000);
        // 设置超时
        requestFactory.setReadTimeout(10000);
        RestTemplate restTemplate = new RestTemplate(requestFactory);
        restTemplate.setErrorHandler(new ResponseErrorHandler() {
            //调用出现错误时捕获异常
            @Override
            public boolean hasError(ClientHttpResponse response) throws IOException {
                // 返回false表示不管response的status是多少都返回没有错(就不进入handleError)
                // 这里可以自己定义那些status code你认为是可以抛Error
                return response.getRawStatusCode() == 200 ? false : true;
            }

            @Override
            public void handleError(ClientHttpResponse response) throws IOException {
                // 这里面可以实现你自己遇到了Error进行合理的处理
                System.out.println("resultInfo:" + response.toString());
                System.out.println("statusCode:" + response.getStatusCode().name());
                System.out.println("statusCodeValue:" + response.getRawStatusCode());
            }
        });
        HttpHeaders headers = new HttpHeaders();
        //设置请求头
        if (header != null) {
            header.forEach((k, v) -> headers.set(k, v.toString()));
        }
        final HttpEntity requestEntity;
        if (isRaw) {
            headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
            requestEntity = new HttpEntity<>(data.toJSONString(), headers);
        } else {
            //设置参数
            MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
            if (data != null) {
                data.forEach((k, v) -> {
                    params.add(k, v.toString());
                });
            }
            requestEntity = new HttpEntity<>(params, headers);
        }
        ResponseEntity<T> exchange = restTemplate.exchange(url, method, requestEntity, c, data == null ? new JSONObject() : data);
        return exchange.getBody();
    }


}

